<template>
  <div class="register-page">
    <div class="register-container">
      <h2>用户注册</h2>
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <input
            v-model="form.username"
            type="text"
            placeholder="请输入用户名"
            required
            class="form-input"
          >
        </div>
        <div class="form-group">
          <input
            v-model="form.email"
            type="email"
            placeholder="请输入邮箱"
            required
            class="form-input"
          >
        </div>
        <div class="form-group">
          <div class="password-input-container">
            <input
              v-model="form.password"
              :type="showPassword ? 'text' : 'password'"
              placeholder="请输入密码"
              required
              class="form-input password-input"
            >
            <button type="button" class="password-toggle" @click="showPassword = !showPassword">
              {{ showPassword ? '隐藏' : '显示' }}
            </button>
          </div>
        </div>
        <div class="form-group">
          <input
            v-model="form.confirmPassword"
            type="password"
            placeholder="请确认密码"
            required
            class="form-input"
          >
        </div>
        
        <!-- 错误提示 -->
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
        
        <button type="submit" class="form-button" :disabled="loading">
          {{ loading ? '注册中...' : '注册' }}
        </button>
      </form>
      
      <div class="form-links">
        <router-link to="/login" class="form-link">已有账号？立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const loading = ref(false)
const showPassword = ref(false)
const errorMessage = ref('')

const form = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

const handleRegister = async () => {
  // 表单验证
  if (!form.username.trim()) {
    errorMessage.value = '请输入用户名'
    return
  }

  if (!form.email.trim()) {
    errorMessage.value = '请输入邮箱'
    return
  }

  if (!form.password) {
    errorMessage.value = '请输入密码'
    return
  }

  if (form.password !== form.confirmPassword) {
    errorMessage.value = '两次输入的密码不一致'
    return
  }

  if (form.password.length < 6) {
    errorMessage.value = '密码长度至少6位'
    return
  }

  errorMessage.value = ''
  loading.value = true

  try {
    // 调用注册方法
    await userStore.register(form)
    
    // 注册成功后自动登录
    await userStore.login({
      username: form.username,
      password: form.password
    })
    
    // 跳转到首页
    router.push('/')
  } catch (error) {
    errorMessage.value = error.message || '注册失败，请重试'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.register-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(100vh - 140px);
  padding: 40px 20px;
}

.register-container {
  width: 100%;
  max-width: 400px;
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.register-container h2 {
  text-align: center;
  margin-bottom: 2rem;
  color: #333;
  font-size: 1.5rem;
}

.register-form {
  margin-bottom: 1.5rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  box-sizing: border-box;
  transition: border-color 0.3s;
}

.form-input:focus {
  outline: none;
  border-color: #007bff;
}

.password-input-container {
  position: relative;
}

.password-input {
  padding-right: 60px;
}

.password-toggle {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.password-toggle:hover {
  background: #f5f5f5;
}

.form-button {
  width: 100%;
  padding: 12px;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
  margin-top: 1rem;
}

.form-button:hover:not(:disabled) {
  background: #0056b3;
}

.form-button:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.form-links {
  text-align: center;
  margin-top: 1rem;
}

.form-link {
  color: #007bff;
  text-decoration: none;
  font-size: 14px;
}

.form-link:hover {
  text-decoration: underline;
}

.error-message {
  color: #dc3545;
  background: #f8d7da;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 1rem;
  text-align: center;
  font-size: 14px;
  border: 1px solid #f5c6cb;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .register-container {
    padding: 1.5rem;
    margin: 1rem;
  }
  
  .register-container h2 {
    font-size: 1.3rem;
  }
}
</style>